package com.jisu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jisu.dto.UserDTO;
import com.jisu.dto.UserQueryDTO;
import com.jisu.mapper.UserMapper;
import com.jisu.pojo.User;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;


import java.util.List;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author fzy
 * @since 2025-07-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String accountNumber, String password) {
        // 使用 QueryWrapper 构造查询条件
        return query()
                .eq("user_account_number", accountNumber)  // 数据库字段名
                .eq("user_password", password)
                .one();
    }

    @Override
    public User getByAccountNumber(String accountNumber) {
        return query().eq("user_acountnumber", accountNumber).one();
    }

    @Override
    public List<User> searchUsers(UserQueryDTO userQueryDTO) {
        return userMapper.selectList(new QueryWrapper<User>()
                .like(StringUtils.isNotBlank(userQueryDTO.getUserName()), "user_name", userQueryDTO.getUserName())
                .like(StringUtils.isNotBlank(userQueryDTO.getRealName()), "real_name", userQueryDTO.getRealName())
                .like(StringUtils.isNotBlank(userQueryDTO.getUserAccountNumber()), "user_account_number", userQueryDTO.getUserAccountNumber())
                .like(StringUtils.isNotBlank(userQueryDTO.getEmail()), "email", userQueryDTO.getEmail())
                .like(StringUtils.isNotBlank(userQueryDTO.getPhone()), "phone", userQueryDTO.getPhone())
                .ge(userQueryDTO.getCreateTimeStart() != null, "create_time", userQueryDTO.getCreateTimeStart())
                .le(userQueryDTO.getCreateTimeEnd() != null, "create_time", userQueryDTO.getCreateTimeEnd())
                .ge(userQueryDTO.getUpdateTimeStart() != null, "update_time", userQueryDTO.getUpdateTimeStart())
                .le(userQueryDTO.getUpdateTimeEnd() != null, "update_time", userQueryDTO.getUpdateTimeEnd()));
    }

    @Override
    public IPage<User> getUsersByPage(int pageNum, int pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return userMapper.selectPage(page, null);  // 第二个参数为查询条件，null 表示无条件

    }


    @Override
    public String checkUserAccountOrPhoneExists(String userAccountNumber, String phone) {
        // 检查 userAccountNumber 是否存在
        if (this.getOne(new QueryWrapper<User>().eq("user_account_number", userAccountNumber)) != null) {
            return "userAccountNumber";
        }

        // 检查 phone 是否存在
        if (this.getOne(new QueryWrapper<User>().eq("phone", phone)) != null) {
            return "phone";
        }

        return null;
    }
}
